<?php

namespace App\Repository;

use App\Base\IRI;
use App\Entity\Department;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\Serializer\SerializerInterface;

/**
 * @method Department|null find($id, $lockMode = null, $lockVersion = null)
 * @method Department|null findOneBy(array $criteria, array $orderBy = null)
 * @method Department[]    findAll()
 * @method Department[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
 */
class DepartmentRepository extends ServiceEntityRepository
{

    public $serializer;
    public function __construct(ManagerRegistry $registry,SerializerInterface $serializer)
    {
        parent::__construct($registry, Department::class);
        $this->serializer = $serializer;
    }


    //获取所有分类
    public function getAllDepartment(){
//        return $this->findBy(array("parent.id"=>['$exists'=>false]),array("sort"=>"ASC"));
        return $this->findBy(array("parent"=>null),array("sort"=>"ASC"));
    }

    //获取所有分类并构建分类树
    public function getAllDepartmentTree()
    {
        $root = $this->getAllDepartment();
//        dump($root);
        $this->createTree($root,$list);
        $data = array();
        $levels = [];
        if (!$list) {
            $list = [];
        }
        /**@var $en Department */
        foreach ($list as $en) {
            if (!$en->getParent()){
                $level = 0;
            }else{
                $level = $levels[$en->getParent()->getId()] + 1;
            }
            $levels[$en->getId()] = $level;
            $prefix = "";
            if ($level > 0) {
//                $prefix .="╘";
//                for ($i=0;$i<$level;$i++ ) {
//                    $prefix .="═";
//                }
//                $prefix .=" ";
                $prefix .="";
                for ($i=0;$i<$level;$i++ ) {
                    $prefix .="└─";
                }
                $prefix .="┤";
            }
            if ($this->serializer) {

                $entity = json_decode($this->serializer->serialize($en,"json"),true);
            }else{

                $entity = IRI::toArray($en);
            }
            $tmp = [
                "level" => $level,
                "data" => $en,
                "pre" => $prefix
            ];
            $data[] = array_merge($entity,$tmp);
        }
        return $data;
    }

    function createTree($objectList , &$rst = [] ,$level = 0)
    {
        foreach ($objectList as $parent) {
            $rst[] = $parent;
            if ($sons= $parent->getSons()) {
                $this->createTree($sons,$rst,$level+1);
            }
        }
    }

    public function getSons4Setting($parent=null)
    {
        $data = $this->getAllDepartmentTree();
        $return = [["id"=>null,'value'=>"请选择"]];
        foreach ($data as $row) {
            if (!$parent) {
                $id =  IRI::createIRIStringFromEntity(Department::class,$row["data"]->getId());
                $value = $row["pre"] . $row["data"]->getName();
                $return[] = ["id"=>$id,'value'=>$value];
            }else{
                if ($row["data"]->getParent() && $row["data"]->getParent()->getId() == $parent) {
//                    $return[ IRI::createIRIStringFromEntity(Department::class,$row["data"]->getId())] = $row["pre"] . $row["data"]->getName();
                    $id =  IRI::createIRIStringFromEntity(Department::class,$row["data"]->getId());
                    $value = $row["pre"] . $row["data"]->getName();
                    $return[] = ["id"=>$id,'value'=>$value];
                }
            }
        }
        return $return ;
    }
    // /**
    //  * @return Department[] Returns an array of Department objects
    //  */
    /*
    public function findByExampleField($value)
    {
        return $this->createQueryBuilder('d')
            ->andWhere('d.exampleField = :val')
            ->setParameter('val', $value)
            ->orderBy('d.id', 'ASC')
            ->setMaxResults(10)
            ->getQuery()
            ->getResult()
        ;
    }
    */

    /*
    public function findOneBySomeField($value): ?Department
    {
        return $this->createQueryBuilder('d')
            ->andWhere('d.exampleField = :val')
            ->setParameter('val', $value)
            ->getQuery()
            ->getOneOrNullResult()
        ;
    }
    */
}
